package org.example;

import cn.hutool.core.date.StopWatch;
import java.util.concurrent.RecursiveTask;

/**
 * @Title : F
 * @Description: :
 * @author: libo@juzishuke.com
 * @date: 2023/10/18 9:20
 * @Version:1.0
 */
public class Fibonacci extends RecursiveTask<Integer> {

    final int n;

    Fibonacci(int n) {
        this.n = n;
    }

    public Integer compute() {
        if (n <= 1) {
            return n;
        }
        Fibonacci f1 = new Fibonacci(n - 1);
        f1.fork();
        Fibonacci f2 = new Fibonacci(n - 2);
        return f2.compute() + f1.join();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            StopWatch sw = new StopWatch();
            sw.start();
            Fibonacci f = new Fibonacci(i);
            int des = f.compute();
            sw.stop();
            System.out.println(i + "____" + sw.getTotalTimeMillis() + "____" + des);
        }

    }
}